package com.aldobucchi.ds.op
{
	import mx.collections.IList;
	
	import com.aldobucchi.ds.DataStructureEvent;
	import com.aldobucchi.ds.container.ContainerDiff;
	import com.aldobucchi.ds.container.IRContainer;

	public class ContainerToListOp extends AbstractContainerToListOp
	{
		

		
		public function ContainerToListOp(iContainer:IRContainer, oList:IList)
		{
			super(iContainer, oList);
			
			refresh();
			
		}
		
		
		

	
		private function refresh( ):void
		{
		
			oList_autoUpdate( false );
			
				oList.removeAll( );
				
				iContainer.walk( walker );
			
			oList_autoUpdate( true );
		
		}
		
		
		
		
		override protected function iContainer_change( event:DataStructureEvent ):void
		{
		
			var diff:ContainerDiff = event.diff as ContainerDiff;
		
			oList_autoUpdate( false );			
			
				diff.walk( walker );
			
			oList_autoUpdate( true );			
		
		}
		
		
		
		
		private function walker( e:*, m:int ):void
		{
		
			var abs:uint 		= Math.abs( m );
			var remove:Boolean 	= ( m != abs );
			
			for ( var i:int=0; i<abs; i++ )
			{
				if ( remove )
					oList.removeItemAt( oList.getItemIndex( e ) );
				else
					oList.addItem( e );										
			}	
		
		
		}
		

		
		
		
	}
}